
/*
HappyFlags_6.pde - Timer commands usually take a channel number and/or a time-value. 

Copyright (c) 2010 Ed Bennett.  All rights reserved. 

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


// The unit of time is is called a blink. The duration 
// of a blink is related to an AVR jiffy. See
// http://en.wikipedia.org/wiki/Jiffy_%28time%29

#include <TimerScheduler.h>

// hardware pins
int HF0 = 13;
int HF1 = 12;
int HF2 = 11;
int HF3 = 10;
int HF4 = 9;
int HF5 = 8;
int HF6 = 7;
int HF7 = 6;

int rate = 7;
int whichOne = 0;
int evenOdd = 0;
int outBitVal7 = 0;

int outval[8]={
  0,0,0,0,0,0,0,0};

int i;

void setup()
{   

  pinMode(HF0, OUTPUT); 
  pinMode(HF1, OUTPUT); 
  pinMode(HF2, OUTPUT);
  pinMode(HF3, OUTPUT); 
  pinMode(HF4, OUTPUT);
  pinMode(HF5, OUTPUT); 
  pinMode(HF6, OUTPUT);
  pinMode(HF7, OUTPUT); 

  Timer.attachPin(0, HF0);    // (channel, Ard. pin number)
  Timer.attachPin(1, HF1); 
  Timer.attachPin(2, HF2); 
  Timer.attachPin(3, HF3); 
  Timer.attachPin(4, HF4);

  // Register user's callback function name with a timer channel.

  // Timer 5 modifies Timer 6 to ramp the whole speed 
  Timer.onTick(5, setLowTime);
  Timer.repeat(5, 1000); 

  Timer.onTick(6, updatePinTimers); //(channelNumber, functionName)
  Timer.repeat(6,100); // (channel, time)

  Timer.onTick(7, randomMode);
  Timer.repeat(7,3000); 

  Timer.start(0);       // (channel)
  Timer.start(1); 
  Timer.start(2); 
  Timer.start(3); 
  Timer.start(4); 
  Timer.start(5); 
  Timer.start(6); 
  Timer.start(7); 

  Timer.begin();        // start the hardware timer
  Serial.begin(1200);
}

/* 
  Timer 7 is being used with a callback, AND with its user flag. 
  The callback is fast, and the user-flag code in loop() uses
  Serial.print() which is slow. It's the best of both worlds running
  on Timer 7.
*/

void loop()
{  
  if(Timer.flagIsSet(7)){ 
    Serial.print("How! High are you? ");
    Serial.println(whichOne);
  }
}

  //callback on timer 7
  void randomMode(void){
    evenOdd ^=1;

    outBitVal7 ^= 1;
    digitalWrite(HF7, outBitVal7); // rollover indicator lamp
  }

//callback on timer 6
// updates one pin per call
void updatePinTimers(void){
  setHighTime(); 

  i++; 
  if(i >= 5){
    i=0;
  }
  whichOne = i; // this is which one

  // if odd, overwrite whichOne with a random value
  if(evenOdd == 1){ 
    whichOne = random(0,5);
  }

  // write the control value to fire the pin
  Timer.mono(whichOne, outval[i]); 
}

void setHighTime(void){  
  if(outval[i]< 5){
    outval[i]= 1000;
  }

  //HIGH TIME ADJUST
  outval[i]= outval[i] * 90/100;
}

//callback on timer 5
// This Timer 5 callback adjusts 
// the repeat rate of Timer 6.
// (example of one timer controlling another timer)

void setLowTime(void){  
  if(rate >300)rate=7;
  rate = rate * 100/85;
  Timer.repeat(6,rate);
}





